#include once "mud-gui/widgets/Canvas.bi"

namespace ui

constructor Canvas ( byval parent as BaseWidget ptr = 0, byval _fgcolor as uinteger = rgb(255,255,255), _
						byval _bgcolor as uinteger = rgb(0,0,0) )

	if parent <> 0 then SetParent(parent)
	setfgColor = _fgcolor
	setBgColor =_bgcolor
	setDrawable = 0

	(BaseObject).id = ui.widgetids.canvas
	(BaseObject).cl = layout_t.noLayout

	connectSignal(@this,clicked,Canvas.click_handler)
	connectSignal(@this,mouseover,Canvas.mouseover_handler)
	connectSignal(@this,mousedown,Canvas.mousedown_handler)
	connectSignal(@this,resize,Canvas.resize_handler)
	connectSignal(@this,repaint,Canvas.PaintEvent)

end constructor

IMP_PROPERTY( Canvas, fgColor, uinteger )
IMP_PROPERTY( Canvas, bgColor, uinteger )
IMP_PROPERTY( Canvas, drawable, ubyte )

property Canvas.imageData( ) as fb.image ptr
	return (BaseObject).buffer
end property

property Canvas.setImageData( byval _t_ as fb.image ptr )
	if (BaseObject).buffer <> 0 then imagedestroy BaseObject.buffer
	(BaseObject).buffer = _t_
end property

property Canvas.minimumWidth( ) as integer
	return (BaseObject).minwidth
end property

property Canvas.setMinimumWidth( byval _t_ as integer )
	(BaseObject).minwidth = _t_
end property

property Canvas.minimumHeight( ) as integer
	return (BaseObject).minheight
end property

property Canvas.setMinimumHeight( byval _t_ as integer )
	(BaseObject).minheight = _t_
end property

sub Canvas.drawCircle(byval x as integer, byval y as integer, byval r as integer, byval clr as uinteger = rgb(255,255,255))

	if (BaseObject).buffer = 0 then return

	circle BaseObject.buffer,(x,y),r,clr
	
end sub

sub Canvas.drawFilledCircle(byval x as integer, byval y as integer, byval r as integer, byval clr as uinteger = rgb(255,255,255))

	if (BaseObject).buffer = 0 then return

	circle BaseObject.buffer,(x,y),r,clr,,,, F

end sub

sub Canvas.drawLine( byval x as integer, byval y as integer, byval x2 as integer, byval y2 as integer, byval clr as uinteger = rgb(255,255,255) )

	if (BaseObject).buffer = 0 then return
	
	line BaseObject.buffer,(x,y)-(x2,y2),clr

end sub

sub Canvas.drawBox( byval x as integer, byval y as integer, byval x2 as integer, byval y2 as integer, byval clr as uinteger = rgb(255,255,255) )

	if (BaseObject).buffer = 0 then return
	
	line BaseObject.buffer,(x,y)-(x2,y2),clr, B

end sub

sub Canvas.drawFilledBox( byval x as integer, byval y as integer, byval x2 as integer, byval y2 as integer, byval clr as uinteger = rgb(255,255,255) )

	if (BaseObject).buffer = 0 then return
	
	line BaseObject.buffer,(x,y)-(x2,y2),clr, BF

end sub

sub Canvas.drawPixel( byval x as integer, byval y as integer, byval clr as uinteger = rgb(255,255,255) )

	if (BaseObject).buffer = 0 then return
	
	pset BaseObject.buffer, ( x, y ), clr

end sub

sub Canvas.clear( )

	if (BaseObject).buffer = 0 then return
	
	line BaseObject.buffer, (0,0)-(BaseObject.width, BaseObject.height),m_bgcolor,BF

end sub

STATIC_SLOT(Canvas.mouseover_handler)

	cast(Canvas ptr,ref)->r_x = e->m_x - ref->x
	cast(Canvas ptr,ref)->r_y = e->m_y - ref->y

	if cast(Canvas ptr,ref)->m_mouseIsDown = 1 andalso cast(Canvas ptr,ref)->m_drawable = 1 then
		pset ref->buffer, (cast(Canvas ptr,ref)->r_x, cast(Canvas ptr,ref)->r_y), iif( cast(Canvas ptr,ref)->m_whatMouseButtonAgain = 1,cast(Canvas ptr, ref)->m_fgcolor,cast(Canvas ptr, ref)->m_bgcolor)
	end if

	return 1

ENDSLOT

STATIC_SLOT(Canvas.mousedown_handler)

	if cast(Canvas ptr,ref)->m_mouseIsDown = 0 then cast(Canvas ptr,ref)->m_mouseIsDown = 1
	cast(Canvas ptr,ref)->m_whatMouseButtonAgain = e->button
	return 1

ENDSLOT

STATIC_SLOT(Canvas.click_handler)

	cast(Canvas ptr,ref)->m_mouseIsDown = 0
	cast(Canvas ptr,ref)->m_whatMouseButtonAgain = 0

	if ref->buffer = 0 then
		ref->buffer = imagecreate( ref->width, ref->height, cast(Canvas ptr, ref)->m_bgcolor )
	end if

	if cast(Canvas ptr,ref)->m_drawable = 1 then
		pset ref->buffer, (cast(Canvas ptr, ref)->r_x, cast(Canvas ptr, ref)->r_y), iif( e->button and 1,cast(Canvas ptr, ref)->m_fgcolor,cast(Canvas ptr, ref)->m_bgcolor)
	end if

	return 1
ENDSLOT

STATIC_SLOT(Canvas.resize_handler)

	var x = imagecreate( ref->width, ref->height, cast(Canvas ptr, ref)->m_bgcolor )
	if ref->buffer <> 0 then
		put x,(0,0),ref->buffer, PSET
		imagedestroy( ref->buffer )
	end if
	ref->buffer = x
	return 0

ENDSLOT

STATIC_SLOT(Canvas.PaintEvent)

	put ( ref->x, ref->y ), ref->buffer, TRANS
	return 0

ENDSLOT

end namespace
